<?php

namespace common\models\trade;

use Yii;
use common\models\shop\Order;
use common\models\shop\OrderRefund;
use common\models\system\AdminUser;
use common\models\User;

/**
 * This is the model class for table "{{%trade_refund}}".
 *
 * @property string $id
 * @property string $user_id
 * @property string $trade_id 交易ID
 * @property string $trade_order_id 退款订单ID （废弃）
 * @property string $order_refund_id 退款申请表id （废弃）
 * @property string $trade_no 退款交易号
 * @property string $from_trade_no 外部退单对应的支付交易号
 * @property string $from_no 外部退单编号
 * @property string $from 退单来源
 * @property string $refund_no 当前表的退款单号
 * @property int $refund_type 退款类型：0全额退款，1部分退款
 * @property string $total 请求退款总金额
 * @property double $cash 退款金额
 * @property double $credit 退款积分
 * @property double $reason 退款原因
 * @property int $status 退款状态
 * @property string $admin_id 操作人员ID
 * @property string $verify_time 退款回调通知时间
 * @property string $passed_time 审核发起时间
 * @property string $created_at
 * @property string $updated_at
 */
class TradeRefund extends \common\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;
    use \common\traits\User;

    /**
     * @var REFUND_TYPE_FULL 全额退款
     * @var REFUND_TYPE_PARTIAL 部分退款
     */
    const REFUND_TYPE_FULL = 0;
    const REFUND_TYPE_PARTIAL = 1;

    /**
     * @var STATUS_WAITPAY 等待支付
     * @var STATUS_SUCCESS 支付完成
     * @var STATUS_SUBMIT  提交支付
     */
    const STATUS_WAITPAY = 0;
    const STATUS_SUCCESS = 1;
    const STATUS_SUBMIT = 2;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%trade_refund}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'trade_id', 'trade_order_id', 'refund_type', 'status', 'admin_id', 'created_at', 'updated_at'], 'integer'],
            [['credit', 'cash'], 'number', 'min' => 0],
            [['total', 'cash', 'credit'], 'number'],
            [['reason'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'trade_id' => '交易ID',
            'trade_order_id' => '退款订单ID',
            'order_refund_id' => '商城退款申请表id',
            'trade_no' => '退款交易号',
            'from_trade_no' => '来源的退款交易号',
            'from_no' => '来源的退款单号',
            'from' => '来源',
            'refund_no' => '当前表的退款单号',
            'refund_type' => '退款类型',
            'total' => '退款总金额',
            'cash' => '退款现金',
            'credit' => '退款积分',
            'reason' => '退款原因',
            'status' => '退款状态',
            'admin_id' => '操作人员ID',
            'verify_time' => '退款回调通知时间',
            'passed_time' => '审核发起时间',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getTrade() {
        return $this->hasOne(Trade::className(), ['id' => 'trade_id'])->inverseOf('orders');
    }

    public function getAdmin() {
        return $this->hasOne(AdminUser::className(), ['id' => 'admin_id']);
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * 因from_trade_no取代 trade_order_id 故此方法废弃不用
     * @return \yii\db\ActiveQuery
     */
    public function getTradeOrder() {
        return $this->hasOne(Trade::className(), ['id' => 'trade_order_id']);
    }

    public function getOrderRefund() {
        return $this->hasOne(OrderRefund::className(), ['id' => 'order_refund_id']);
    }

    public function getFromTrade() {
        return $this->hasOne(Trade::className(), ['trade_no' => 'from_trade_no']);
    }

    /**
     * @return string
     * 生成退款单号
     */
    public function generateTn() {
        do {
            $tn = 'TR' . date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            $record = static::findOne(['refund_no' => $tn]);
        } while ($record);
        $this->refund_no = $tn;
        return $this->refund_no;
    }

}
